<?PHP
	/************************************************************************
	*       This file, in addition to all other parts of X7 Chat are        *
	*                   Copyright 2003-2007 by Tim Chamness.                *
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	* Version:          X7 Chat 3.0.0-A2                                    *
	* Release Date:     October 20, 2007                                    *
	* Author:           Tim Chamness                                        *
	* Copyright:        2003-2007 by Tim Chamness                           *
	* Website:          http://www.x7chat.com/                              *
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	* X7 Chat 3 is free software; you can redistribute it and/or modify     *
	* it under the terms of the GNU General Public License as published by  *
	* the Free Software Foundation; either version 3 of the License, or     *
	* (at your option) any later version.                                   *
	*                                                                       *
	* X7 Chat 3 is distributed in the hope that it will be useful, but      *
	* WITHOUT ANY WARRANTY; without even the implied warranty of            *
	* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
	* General Public License for more details.                              *
	*                                                                       *
	* You should have received a copy of the GNU General Public License     *
	* along with this program.  If not, see <http://www.gnu.org/licenses/>. *
	*                                                                       *
	* Additional Terms, compatible with Section 7 of the GNU GPL v3,        *
	* apply to X7 Chat 3 and all X7 Chat 3 files, including this one.       *
	* You should have received a copy of these Additional Terms in the      *
	* file docs/COPYING.ADDITIONAL_TERMS.  If you did not receive a copy    *
	* please visit <http://www.x7chat.com/COPYING.ADDITIONAL_TERMS/>.       *
	************************************************************************/

	// Make sure this file isn't being accessed directly
	if(!defined('IN_X7CHAT3')) return;
	
	// See if registration is disabled
	if(!$settings['allow_registration']){
		
		// Try to present a helpful error message
		if($settings['allow_guests'])
			$msg = $GLOBALS['LANG']->lang_intro->register_disable_guests_enable;
		elseif(!empty($settings['alt_registration_url']))
			$msg = $GLOBALS['LANG']->format($GLOBALS['LANG']->lang_intro->register_disable_register_elsewhere,array('url',"<a href='{$settings['alt_registration_url']}' target='_blank'>{$settings['alt_registration_url']}</a>"));
		else
			$msg = $GLOBALS['LANG']->lang_intro->register_disable;
		
		echo tpl_register::registration_disabled($msg);
		return;
		
	}
	
	// Create the registration form
	$form = new x7form();
	
	$form->add_section(1,$GLOBALS['LANG']->lang_intro->register,$GLOBALS['LANG']->lang_intro->register_intro);
	$form->add_field_text(1,'uname',$GLOBALS['LANG']->lang_intro->username);
	$form->add_field_pass(1,'pword1',$GLOBALS['LANG']->lang_intro->password);
	$form->add_field_pass(1,'pword2',$GLOBALS['LANG']->lang_intro->confirm_password);
	$form->add_field_text(1,'email',$GLOBALS['LANG']->lang_intro->email);
	
	$res = $form->get_results();
	if(!empty($res)){
		
		// Check username for validity
		$check_uname = lib_user_extended::validate_username($res['uname']);
		if(!empty($check_uname))
			$form->add_field_error('uname',$check_uname,'');
		
		// Check password for validity
		if($res['pword1'] !== $res['pword2'])
			$form->add_field_error('pword2',$GLOBALS['LANG']->lang_intro->new_password_error,'');
		else{
			$check_pword = lib_user_extended::validate_password($res['pword1']);
			if(!empty($check_pword))
				$form->add_field_error('pword1',$check_pword,'');
		}
			
		// Check E-Mail for validity
		$check_email = lib_user_extended::validate_email($res['email']);
		if(!empty($check_email))
			$form->add_field_error('email',$check_email,'');
		
		// See if any errors exist
		if(!$form->has_errors()){
			
			$uid = $GLOBALS['DATABASE']->table_next_id('users');
			
			$new_user = new lib_user($uid);
			$new_user->data_cache = array(	
							'uid'=>$uid,
							'name'=>$res['uname'],
							'group'=>2,
							'password'=>sha1($res['pword1']),
							'ip'=>$_SERVER['REMOTE_ADDR'],
							'profile'=>array(),
							'email'=>$res['email'],
							'buddies'=>array(),
							'ignore'=>array(),
							'activation_code'=>0,
							'reset_code'=>'',
							'status'=>'',
							'settings'=>array('styles'=>array('font'=>'','size'=>'','color'=>''),'show_timestamps'=>1,'show_smilies'=>1,'play_sounds'=>1,'show_styles'=>1,'hide_email'=>1),
							'perms'=>array());
			
			// Account creation successful, redirect to appropriate location
			if($settings['account_activation'] == 'none' || ($settings['account_activation'] == 'user' && !$settings['send_welcome_email'])){
				$new_user->commit_changes();
				// Simulate a login
				$_POST['lgnusername'] = $new_user->data_cache['name'];
				$_POST['lgnpassword'] = $res['pword1'];
				$ses = new lib_session();
				header("location: ./index.php?{$ses->session_string}");
			}else{
				// Add activation code
				$activation_code = ($settings['account_activation'] == 'user' ? sha1(microtime(true)*rand(2,1001)) : 1);
				$new_user->data_cache['activation_code'] = $activation_code;
				$GLOBALS['DATABASE']->put('activation',array('uid'=>$new_user->data_cache['uid'],'name'=>$new_user->data_cache['name']));
				$new_user->commit_changes();
			}
			
			$email_status = lib_email::E_NONE;
			if(!empty($email) && $settings['send_welcome_email'])
				$email_status = lib_user_extended::send_welcome_email($new_user);
			
			if($settings['account_activation'] == 'user' && $email_status !== lib_email::E_NONE)
				$msg = $GLOBALS['LANG']->lang_intro->account_inactive_emailfail;
			elseif($settings['account_activation'] == 'user')
				$msg = $GLOBALS['LANG']->format($GLOBALS['LANG']->lang_intro->account_inactive,array('ecode',$email_status));
			elseif($settings['account_activation'] == 'admin')
				$msg = $GLOBALS['LANG']->lang_intro->account_inactive_admin;
			else
				$msg = $GLOBALS['LANG']->lang_intro->account_added;
			
			echo tpl_register::account_created($msg);
			return;
			
		}
		
	}
	
	echo tpl_register::register_page($form);
	return;
?>